<?php

namespace app\services;

use app\model\CustomerCreditCard;
use app\model\CustomerInfo;
use app\model\CustomerLoanAccount;

class NoticeService extends ApiService
{
    protected $customerInfo;
    protected $customerCreditCard;
    protected $customerLoanAccount;

    public function __construct(array $userInfo = [], array $params = [])
    {
        parent::__construct($userInfo, $params);
        $this->customerInfo = new CustomerInfo();
        $this->customerCreditCard = new CustomerCreditCard();
        $this->customerLoanAccount = new CustomerLoanAccount();
    }

    //生日提醒
    public function birthdayNotice()
    {
        $noticeList = ['notice_list' => []];
        if (!$list = $this->customerInfo->birthdayNotice()) {
            return $noticeList;
        }
        foreach ($list as $item) {
            $noticeList['notice_list'][] = [
                'customer_id' => $item['customer_id'],
                'birthday_notice' => $item['username'] . '---' . $item['birthday']
            ];
        }
        return $noticeList;
    }

    public function expireNotice()
    {
        $noticeList = ['notice_list' => []];
        if (!$list = $this->customerLoanAccount->expireNotice()) {
            return $noticeList;
        }
        $data = $temp = [];
        foreach ($list as $item) {
            if (isset($temp[$item['customer_id']])) {
                $temp[$item['customer_id']][] = $item['organization_name'] . '的有一笔贷款。贷款总额度为' .
                    $item['total_amount'] .
                    '。' . $item['end_time'] . '到期，还款方式为：' . $item['repayment_style'];
            } else {
                $temp[$item['customer_id']] = [
                    $item['organization_name'] . '的有一笔贷款。贷款总额度为' .
                    $item['total_amount'] . '。' . $item['end_time'] . '到期，还款方式为：' . $item['repayment_style']
                ];
            }
        }
        $customerList = $this->customerInfo->idKeyList();
        foreach ($temp as $key => $value) {
            $data[] = [
                'username' => $customerList[$key]['username'],
                'repayment_notice' => $value
            ];
        }
        $noticeList['notice_list'] = $data;
        return $noticeList;
    }

    //信用卡还款提醒
    public function creditCardRepaymentNotice()
    {
        $noticeList = ['notice_list' => []];
        if (!$list = $this->customerCreditCard->repaymentNoticeList()) {
            return $noticeList;
        }
        $data = $temp = [];
        foreach ($list as $item) {
            if (isset($temp[$item['customer_id']])) {
                $temp[$item['customer_id']][] = $item['organization_name'] . '的信用卡，账单日为' . $item['bill_day'] .
                    '号。您可以提前把他还进去，账单日过了再支用下来。';
            } else {
                $temp[$item['customer_id']] = [
                    $item['organization_name'] . '的信用卡，账单日为' . $item['bill_day'] .
                    '号。您可以提前把他还进去，账单日过了再支用下来。'
                ];
            }
        }
        $customerList = $this->customerInfo->idKeyList();
        foreach ($temp as $key => $value) {
            $data[] = [
                'username' => $customerList[$key]['username'],
                'repayment_notice' => $value
            ];
        }
        $noticeList['notice_list'] = $data;
        return $noticeList;
    }

    //信用卡支用提醒
    public function creditCardPayNotice()
    {
        $noticeList = ['notice_list' => []];
        if (!$list = $this->customerCreditCard->payNoticeList()) {
            return $noticeList;
        }
        $data = $temp = [];
        foreach ($list as $item) {
            if (isset($temp[$item['customer_id']])) {
                $temp[$item['customer_id']][] = $item['organization_name'] . '的信用卡，账单日为' . $item['bill_day'] .
                    '号。账单日已经过了，如果用得到可以把他刷出来备用';
            } else {
                $temp[$item['customer_id']] = [
                    $item['organization_name'] . '的信用卡，账单日为' . $item['bill_day'] .
                    '号。账单日已经过了，如果用得到可以把他刷出来备用'
                ];
            }
        }
        $customerList = $this->customerInfo->idKeyList();
        foreach ($temp as $key => $value) {
            $data[] = [
                'username' => $customerList[$key]['username'],
                'repayment_notice' => $value
            ];
        }
        $noticeList['notice_list'] = $data;
        return $noticeList;
    }

    public function repaymentNotice()
    {
        $noticeList = ['notice_list' => []];
        if (!$list = $this->customerLoanAccount->repaymentNotice()) {
            return $noticeList;
        }
        $data = $temp = [];
        foreach ($list as $item) {
            if (isset($temp[$item['customer_id']])) {
                $temp[$item['customer_id']][] = $item['organization_name'] . '的有一笔贷款，贷款总额度为' . $item['total_amount'] .
                    '元。后天就是您的还款日了，注意提前将钱存入对应的银行卡号哦，以免逾期！';
            } else {
                $temp[$item['customer_id']] = [
                    $item['organization_name'] . '的有一笔贷款，贷款总额度为' . $item['total_amount'] .
                    '元。后天就是您的还款日了，注意提前将钱存入对应的银行卡号哦，以免逾期！'
                ];
            }
        }
        $customerList = $this->customerInfo->idKeyList();
        foreach ($temp as $key => $value) {
            $data[] = [
                'username' => $customerList[$key]['username'],
                'repayment_notice' => $value
            ];
        }
        $noticeList['notice_list'] = $data;
        return $noticeList;
    }
}